<?php
// 每天短信发送次数获取与添加, 包含IP，手机号

namespace App\Http\Models;

use Illuminate\Support\Facades\Redis;;

class SMSTimesModel
{
	
	const SMS_IP_TIMES_KEY = 'smstimes:ip:';
	const SMS_MOBILE_TIMES_KEY = 'smstimes:mobile:';
	const EXPIRES_TIME = '86400';
	

    // set token
	public static function add_ip_times($ip)
	{
		$redis = Redis::connection('db_main');
		$timestamp = date("Ymd",time());
		return $redis->incr(self::SMS_IP_TIMES_KEY . $timestamp . ":" . ip2long($ip));
	}

	public static function add_mobile_times($mobile)
	{
		$redis = Redis::connection('db_main');
		$timestamp = date("Ymd",time());
		return $redis->incr(self::SMS_MOBILE_TIMES_KEY . $timestamp . ":" . $mobile);
	}

	// get token
	public static function get_ip_times($ip)
	{
		$redis = Redis::connection('db_main');
		$timestamp = date("Ymd",time());
		$key = self::SMS_IP_TIMES_KEY . $timestamp . ':' . ip2long($ip);
		$times = $redis->get($key);
		if($times == null) {
			$times = 0;
			$redis->setex($key, self::EXPIRES_TIME, $times); 
		}
		return $times;
	}
	
	public static function get_mobile_times($mobile)
	{
		$redis = Redis::connection('db_main');
		$timestamp = date("Ymd",time());
		$key = self::SMS_MOBILE_TIMES_KEY . $timestamp . ':' . $mobile;
		$times = $redis->get($key);
		if($times == null) {
			$times = 0;
			$redis->setex($key, self::EXPIRES_TIME, $times); 
		}
		return $times;
	}
}

